package com.home.cop.sysinfo.controller;

import com.home.cop.base.BaseController;
import com.home.cop.employee.model.Employee;
import com.home.cop.employee.model.SysMenu;
import com.home.cop.employee.model.SysUser;
import com.home.cop.enums.sysinfo.EmployeeStatusEnum;
import com.home.cop.enums.sysinfo.UserStatusEnum;
import com.home.cop.sysinfo.model.*;
import com.home.cop.employee.service.EmployeeService;
import com.home.cop.sysinfo.service.SysDataDictService;
import com.home.cop.sysinfo.service.SysMenuService;
import com.home.cop.employee.service.SysUserService;
import com.home.cop.utils.MD5Utils;
import com.home.cop.system.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@Api("登录接口")
public class LoginController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private SysDataDictService dictService;

    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "登录", notes = "根据账号密码登录")
    @ApiImplicitParams({ @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型 0-系统用户，1-普通员工", required = true, dataType = "Integer", paramType = "query")
    })
    public Result login(@RequestParam("account") String sysUserAcct, @RequestParam("pwd") String sysUserPasswd,@RequestParam("type") Integer type){
        String pass = MD5Utils.MD5(sysUserPasswd);
        Result result = new Result();
        UserSession userSession = new UserSession();
        HttpSession session = getRequest().getSession();
        Integer roleId = -1;
        /*系统用户处理*/
        if (type == 0){
            SysUser user = userService.getUser(sysUserAcct,pass);
            if (user == null){
                return Result.returnFail().setMessage("密码错误！");
            }
            if(user.getSysUserStatus() == UserStatusEnum.DISABLE.getCode()){
                return Result.returnFail().setMessage("用户被禁用！");
            }
            roleId = user.getRoleId();
            if(user.getInstId() == 0){
                roleId = -1;
            }
            userSession.setInstId(user.getInstId());
            userSession.setAccount(user.getSysUserAcct());
            userSession.setName(user.getSysUserName());
            userSession.setInstName(user.getInstName());
            userSession.setRoleId(user.getRoleId());
            userSession.setType(type);
            session.setAttribute("userSession",userSession);
        }else {
            /*普通员工*/
            Employee employee = employeeService.getEmployee(sysUserAcct,pass);
            if (employee == null){
                return Result.returnFail().setMessage("密码错误！");
            }
            if(employee.getEmpStatus() == EmployeeStatusEnum.DISABLE.getCode()){
                return Result.returnFail().setMessage("用户被禁用！");
            }
            roleId = employee.getRoleId();
            if(employee.getInstId() == 0){
                roleId = -1;
            }
            userSession.setInstId(employee.getInstId());
            userSession.setAccount(employee.getEmpAcct());
            userSession.setName(employee.getEmpName());
            userSession.setInstName(employee.getInstName());
            userSession.setRoleId(employee.getRoleId());
            userSession.setType(type);
            session.setAttribute("userSession",userSession);
        }
        result.pushData("user",userSession);
        List<SysMenu> menus = new ArrayList<>();
        if (roleId == -1){
            menus = menuService.getAllSysMenu();
        }else {
            menus = menuService.getSysMenuByRoleId(roleId);
        }
        List<SysDataDict> dicts = dictService.list(null);
        result.pushData("menus",menus).pushData("dicts",dicts);
        result.setSuccess(true);
        return result;
    }

    /**
     * 根据电话号码获取机构
     * @param phone
     * @param type 0-系统用户，1-员工
     * @return
     */
    @PostMapping("/getUsersByPhone")
    @ResponseBody
    @ApiOperation(value = "根据电话号码获取机构信息", notes = "根据电话号码获取机构信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "电话号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型 0-系统用户，1-普通员工", required = true, dataType = "Integer", paramType = "query")
    })
    public Result getUsersByPhone(@RequestParam("phone") String phone,@RequestParam("type") Integer type){
        if (StringUtils.isBlank(phone)){
            return Result.returnFail().setMessage("请输入账号！");
        }
        if(type == 1){
            List<Employee> employees = employeeService.getEmployeeByPhone(phone);
            if (employees == null || employees.size() == 0){
                return Result.returnFail().setMessage("没有该用户！");
            }else {
                return Result.returnSuccess().setData(employees);
            }
        }else {
            List<SysUser> users = userService.getUserByPhone(phone);
            if (users == null || users.size() == 0){
                return Result.returnFail().setMessage("没有该用户！");
            }else {
                return Result.returnSuccess().setData(users);
            }
        }
    }

}
